/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.tools {
	import com.googlecode.milib.base.IMLBroadcaster;
	import com.googlecode.milib.data.MLCollection;
	import com.googlecode.milib.tools.MLEventsReflector;
	import com.googlecode.milib.vo.MLEvent;

	/**
	 * @author Marek Brun
	 */
	public class MLEventsReflectorFromCollection extends MLEventsReflector {

		private var collection:MLCollection;
		
		public function MLEventsReflectorFromCollection(weakReferences:Boolean, collection:MLCollection) {
			super(weakReferences);
			this.collection=collection;
			collection.addEventListener(collection.event_ValueAdded, onCollectionValueAdded);			collection.addEventListener(collection.event_ValueRemoved, onCollectionValueAdded);			collection.addEventListener(collection.event_Cleared, onCollectionCleared);
		}
		
		override public function addObject(obj:IMLBroadcaster, $data:*=null):void {
			throw Error('You can add objects only by MLCollection instance passed in constructor');
		}
		override public function removeObject(obj:IMLBroadcaster):void {
			throw Error('You can remove objects only by MLCollection instance passed in constructor');
		}
		override public function clear():void {
			throw Error('You can clear only by MLCollection instance passed in constructor');
		}
		
//********************************************************************************************
//		events for MLEventsReflectorFromCollection
//********************************************************************************************
		protected function onCollectionValueAdded(event:MLEvent):void {
			super.addObject(event.info.data);
		}
		
		protected function onCollectionValueRemoved(event:MLEvent):void {
			super.removeObject(event.info.data);
		}
		
		protected function onCollectionCleared(event:MLEvent):void {
			super.clear();
		}
		
	}
}
